var express = require('express');
const Result = require('../beans/result');
var router = express.Router();
const categoriesController = require("../controllers/categories_controller")
const mongoUtil = require("../models/mongo_util")

// 获取分类
router.get("/shop-cart", (req, res) => {
    categoriesController.getNewbeeCategories((result) => {
        res.json(result)
    })
})
//商品分类模糊查询
router.get("/search/:searchName", (req, res) => {
    // console.log(req.params.searchName);
    var {
        searchName
    } = req.params
    categoriesController.getCategoriesGoods((result) => {


        var searchList = []
        var reg = new RegExp(searchName);


        if (result.code == 200) {
            result.data.forEach(item => {

                for (var key in item.data.data) {
                    item.data.data[key].forEach(ite => {
                        // console.log(reg);

                        console.log(reg.test(ite.goodsName));
                        if (reg.test(ite.goodsName) == true) {

                            searchList.push(ite)
                            console.log(ite);
                        }
                    })
                }
            })
            if (searchList.length > 0) {
                res.json({
                    code: 200,
                    data: searchList,
                    mag: "获取数据成功"

                })
            } else {
                res.json({
                    code: 200,
                    data: searchList,
                    mag: "获取的数据不存在"
                })
            }

        } else {
            res.json({
                code: 401,
                data: [],
                mag: "获取数据失败"
            })
        }
    })
})

// 添加一级分类
var Categories = {
    categoryId: 1,
    name: "maojin",
    price: 100
}
router.post("/Categories", (req, res) => {
    var {
        //Categories
    } = req.body
    categoriesController.AddCategorieslist(Categories, (result) => {
        res.json(result)
    })
})
// 修改一级分类
var putCategories = {
    categoryId: 1,
    name: "feiwu1",
    price: 100
}
router.put("/Categories", (req, res) => {
    var {
        // putCategories

    } = req.body
    categoriesController.putCategorieslist(putCategories, (result) => {
        res.json(result)
    })
})
// 删除一级分类
var categoryId = [1]

router.delete("/Categories", (req, res) => {
    var {
        // categoryId
    } = req.body
    categoriesController.deleteCategorieslist(categoryId, (result) => {
        res.json(result)
    })
})
//添加二级标题
var secondCategories = {
    categoryId: 2,
    parentId: 1,
    name: "xiaolaji"
}
router.post("/secondCategories", (req, res) => {
    var {
        //secondCategories
    } = req.body
    categoriesController.AddsecondCategorieslist(secondCategories, (result) => {
        res.json(result)
    })
})
//修改二级标题
var putsecondCategories = {
    categoryId: 2,
    parentId: 1,
    name: "xiao"
}
router.put("/secondCategories", (req, res) => {
    var {
        // putsecondCategories

    } = req.body
    categoriesController.putsecondCategorieslist(putsecondCategories, (result) => {
        res.json(result)
    })
})
//删除二级标题
var secondCategoriesId = [2]

router.delete("/secondCategories", (req, res) => {
  var {
    // remgoodsId
  } = req.body
  categoriesController.deletesecondCategorieslist(secondCategoriesId, (result) => {
    res.json(result)
  })
})
module.exports = router;